package com.dankegongyu.lib.common.util;

import android.os.Build;
import androidx.core.app.SharedElementCallback;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.view.DraweeTransition;

import java.util.List;

/**
 * 共享元素动画 https://www.jianshu.com/p/e7a9ef7e38e6
 *
 * @author wupuquan
 * @version 1.0
 * @since 2018/12/26 15:23
 */
public class DKSharedElementUtil {

    private DKSharedElementUtil() {
    }

    public static void init(AppCompatActivity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && activity != null) {
            activity.getWindow().setSharedElementEnterTransition(DraweeTransition.createTransitionSet(ScalingUtils.ScaleType.CENTER_CROP, ScalingUtils.ScaleType.CENTER_CROP)); // 进入
            activity.getWindow().setSharedElementReturnTransition(DraweeTransition.createTransitionSet(ScalingUtils.ScaleType.CENTER_CROP, ScalingUtils.ScaleType.CENTER_CROP)); // 返回
        }
    }

    public static void setCallback(AppCompatActivity activity) {
        if (activity == null) {
            return;
        }
        activity.setExitSharedElementCallback(new SharedElementCallback() {
            @Override
            public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
                super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots);
                for (View view : sharedElements) {
                    if (view != null) {
                        view.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }
}
